package com.bjesc_b2c.app.appraiset.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.bjesc_b2c.app.appraiset.widget.dialog.LoadingDialog;
import com.orhanobut.logger.Logger;
import com.rxretrofit_build.model.RxBusBean;

import butterknife.ButterKnife;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
import rxbus_base.RxBus;

/**
 * Created by Jason Chen on 2017/2/20.
 */

public class BaseFragment extends Fragment {
    protected View contentView;
    protected Toast toast;
    protected Context mContext;
    private Subscription subscription;
    private LoadingDialog mLoading;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mContext =activity;
    }
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mContext = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
        return contentView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        Logger.d("onViewCreated");
        initRxBus(setOnNext());
        ButterKnife.bind(this, view);
        super.onViewCreated(view, savedInstanceState);
    }

    /**
     * 初始化View
     */
    public void initView() {

    }
    /**
     * 初始化数据
     */
    public void initData() {

    }

    //此方法只执行一次
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        //只要activity 不销毁,此方法 就只调用一次
        initView();
        initData();
        super.onActivityCreated(savedInstanceState);
    }

    protected void showToast(String text){
        if(toast == null){
            toast = Toast.makeText(getActivity(),text,Toast.LENGTH_LONG);
        }else{
            toast.setText(text);
        }
        toast.show();
    }
    protected void startIntent(Intent intent){
        startActivity(intent);
    }

    protected void startIntent(Intent intent,int requestId){
        startActivityForResult( intent, requestId);
    }
    protected final void showLoading(@StringRes int textResId) {
        showLoading(getString(textResId));
    }
    protected final void showLoading(String text) {
        cancelLoading();
        if (mLoading == null) {
            mLoading = new LoadingDialog(mContext);
            mLoading.setCancelable(false);
            mLoading.setCanceledOnTouchOutside(false);
        }
        mLoading.setTitle(text);
        mLoading.show();
    }
    protected final void showLoading() {
        cancelLoading();
        if (mLoading == null) {
            mLoading = new LoadingDialog(mContext);
            mLoading.setCancelable(false);
            mLoading.setCanceledOnTouchOutside(false);
        }
        mLoading.setTitle("请等待...");
        mLoading.show();
    }
    protected final void cancelLoading() {
        if (mLoading != null && mLoading.isShowing()) {
            mLoading.dismiss();
        }
    }
    protected void initRxBus(final Action1<Object> onNext) {
        if (onNext != null) {
            subscription = RxBus
                    .getInstance()
                    .toObserverable()
                    .onBackpressureDrop()
                    .subscribeOn(Schedulers.io())
                    .unsubscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(onNext);
        }
    }

    protected Action1<Object> setOnNext() {
        return new Action1<Object>() {
            @Override
            public void call(Object o) {
                if (o instanceof RxBusBean) {
                    RxBusBean rxBusBean = (RxBusBean) o;
                    if (!TextUtils.isEmpty(rxBusBean.getRequestMark())) {
                        doOnNext(rxBusBean);
                    }
                }
            }
        };
    }

    protected void doOnNext(RxBusBean rxBusBean) {

    }
    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(getActivity());
        if (subscription != null) {
            subscription.unsubscribe();
        }
    }
}
